package com.baoxin.chenxixc4coach.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.bean.Course;

import java.util.List;

public class CourseAdapter extends BaseAdapter {
    List<Course> courseList;


    public void setCourseList(List<Course> courseList) {
        this.courseList = courseList;
    }

    @Override
    public Object getItem(int position) {
        return courseList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getCount() {
        return courseList == null ? 0 : courseList.size();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_item, parent, false);
            holder = new Holder();
            holder.view = convertView;
            holder.period_tv = convertView.findViewById(R.id.course_period);
            holder.number_tv = convertView.findViewById(R.id.course_number);
            holder.subject_tv = convertView.findViewById(R.id.subject);
            holder.price_tv = convertView.findViewById(R.id.course_price);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        Course course = courseList.get(position);
        holder.period_tv.setText(course.getPeriod());
        holder.number_tv.setText(course.getOfadAlreadyNum() + "/" + course.getOfadMaxNum());

        holder.price_tv.setText("￥" + course.getOfadPrice());
        double subject = course.getSubject();
        if (subject == 2) {
            holder.subject_tv.setText("科目二"+subject);
        } else {
            holder.subject_tv.setText("科目三"+subject);
        }
        int ofadState = course.getOfadState();

        if (ofadState == 3) {//已过期
            holder.period_tv.setTextColor(Color.BLACK);
            holder.number_tv.setTextColor(Color.BLACK);
            holder.subject_tv.setTextColor(Color.BLACK);
            holder.price_tv.setTextColor(Color.BLACK);
            holder.view.setBackgroundResource(R.drawable.course_bg_end);
        } else if (ofadState == 1) {//进行中
            holder.period_tv.setTextColor(Color.WHITE);
            holder.number_tv.setTextColor(Color.WHITE);
            holder.subject_tv.setTextColor(Color.WHITE);
            holder.price_tv.setTextColor(Color.WHITE);
            holder.view.setBackgroundResource(R.drawable.course_bg_ing);
        } else if (ofadState == 2) {//满人
            holder.period_tv.setTextColor(Color.WHITE);
            holder.number_tv.setTextColor(Color.WHITE);
            holder.subject_tv.setTextColor(Color.WHITE);
            holder.price_tv.setTextColor(Color.WHITE);
            holder.view.setBackgroundResource(R.drawable.course_bg_full);
        } else if (ofadState == 0) {//未发布
            holder.period_tv.setTextColor(Color.WHITE);
            holder.number_tv.setTextColor(Color.WHITE);
            holder.subject_tv.setTextColor(Color.WHITE);
            holder.price_tv.setTextColor(Color.WHITE);
            holder.view.setBackgroundResource(R.drawable.course_bg_succeed);
        }
        return convertView;
    }

    class Holder {
        View view;
        TextView period_tv;
        TextView number_tv;
        TextView subject_tv;
        TextView price_tv;
    }
}
